package cn.com.czpb.client.module.personal.adapter;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.annotation.StyleRes;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.com.czpb.client.R;
import cn.com.czpb.client.api.InterfaceJsonfile;
import cn.com.czpb.client.model.news.ComResultBean;
import cn.com.czpb.client.model.news.ComResultEntity;
import cn.com.czpb.client.model.news.CommBean;
import cn.com.czpb.client.model.news.NewsBean;
import cn.com.czpb.client.module.news.activity.NewsAlbum_Activity;
import cn.com.czpb.client.module.news.activity.NewsDetailActivity;
import cn.com.czpb.client.module.news.activity.NewsVideoActivity;
import cn.com.czpb.client.module.news.activity.ZhuantiActivityNew;
import cn.com.czpb.client.module.personal.acticity.LevelActivity;
import cn.com.czpb.client.net.Factory;
import cn.com.czpb.client.utils.AAnim;
import cn.com.czpb.client.utils.DeviceUtils;
import cn.com.czpb.client.utils.LogUtils;
import cn.com.czpb.client.utils.PageCtrl;
import cn.com.czpb.client.utils.ParamUtils;
import cn.com.czpb.client.utils.SPUtil;
import cn.com.czpb.client.utils.TUtils;
import cn.com.czpb.client.utils.TextUtils;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.functions.Func1;
import rx.schedulers.Schedulers;

/**
 * Created by Administrator on 2017/8/14.
 */

public class DynamicsAdapter extends BaseMultiItemQuickAdapter<CommBean, BaseViewHolder> {
    private SPUtil spu;

    private List<CommBean.ChildrenEntity> list;

    private String cid;
    private String nid;
    private String type;
    private boolean flag;
    private Context context;


    /**
     * Same as QuickAdapter#QuickAdapter(Context,int) but with
     * some initialization data.
     *
     * @param data A new list is created out of this one to avoid mutable list
     */
    public DynamicsAdapter(List<CommBean> data, Context context) {
        super(data);
        spu = SPUtil.getInstance();
        this.context = context;
        list = new ArrayList<>();
        addItemType(0, R.layout.item_dynamic);
    }

    @Override
    protected void convert(final BaseViewHolder helper, final CommBean item) {
        if (item.getMediatype().equals("1")){
            helper.getView(R.id.vip).setVisibility(View.VISIBLE);
        }
        switch (helper.getItemViewType()) {
            case 0:
                nid=item.getData().getNid();
                type=item.getData().getType();
                Glide.with(mContext).load(item.getAvatar_path()).into((ImageView) helper.getView(R.id.iv_user_comm));
                helper.setText(R.id.tv_name_comm, TextUtils.getLengthText(item.getNickname(),8));
                helper.setText(R.id.tv_lv_comm, item.getUlevel());
                helper.setText(R.id.tv_content_comm, item.getData().getComment());
                helper.setText(R.id.tv_time_comm, item.getCreate_time());
                helper.setText(R.id.tv_dianzan_comm, item.getData().getP_num());
                helper.setText(R.id.tv_title,"原文："+ item.getData().getTitle());


                final ImageView iv_Praise = helper.getView(R.id.iv_dianzan);
                final TextView tv_Praise = helper.getView(R.id.tv_dianzan_comm);
                if (item.getData().getIspraise().equals("0")) {
                    tv_Praise.setTextColor(mContext.getResources().getColor(R.color.color_999999));
                    iv_Praise.setImageResource(R.drawable.dianzan);
                } else {
                    tv_Praise.setTextColor(mContext.getResources().getColor(R.color.color_df3031));
                    iv_Praise.setImageResource(R.drawable.dianzan_selected);
                }
                helper.addOnClickListener(R.id.tv_reply_comm);
//                helper.getView(R.id.tv_reply_comm).setOnClickListener(new View.OnClickListener() {
//                    @Override
//                    public void onClick(View view) {
//                        cid = item.getData().getCid();
//                        ReplyDialog dialog = new ReplyDialog(mContext, R.style.ChangeNamedialog);
//                        dialog.show();
//                    }
//                });
                helper.getView(R.id.ll_dianzan).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if ("0".equals(item.getData().getIspraise())) {
                            Priase(item.getData().getCid(), tv_Praise, iv_Praise);
                        } else {
                            TUtils.toast("您已点赞");
                        }
                    }
                });
//                helper.getView(R.id.ll_user).setOnClickListener(new View.OnClickListener() {
//                    @Override
//                    public void onClick(View view) {
//                        Intent intent = new Intent();
//
//                        if ("1".equals(item.getMediatype())) {
//                            intent.putExtra("focusUid", item.getUid());
//                            intent.setClass(context, SubscribeDetailActivity.class);
//                        } else {
//                            intent.putExtra("uid", item.getUid());
//                            intent.setClass(context, PersonPageActivity.class);
//                        }
//                        mContext.startActivity(intent);
//                        AAnim.ActivityStartAnimation((Activity) context);
//                    }
//                });
                helper.getView(R.id.tv_lv_comm).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Intent intent = new Intent(context, LevelActivity.class);
                        intent.putExtra("uid", item.getUid());
                        mContext.startActivity(intent);
                        AAnim.ActivityStartAnimation((Activity) context);
                    }
                });
//                helper.getView(R.id.tv_content_comm).setOnLongClickListener(new View.OnLongClickListener() {
//                    @Override
//                    public boolean onLongClick(View view) {
//                        TextView tv = helper.getView(R.id.tv_content_comm);
//                        tv.setBackgroundColor(mContext.getResources().getColor(R.color.color_d4d4d4));
//                        String msg = tv.getText().toString();
//                        CommPop cp = new CommPop((Activity) context, msg, tv, 0);
//                        cp.showAsPullUp(tv, 0, 0);
//                        TUtils.toast(item.getAction()+"-----action");
//                        return true;
//                    }
//
//                });
                helper.getView(R.id.tv_title).setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View view) {
                        OnClick(item);
                    }
                });
                break;
        }
    }
    private void OnClick(CommBean item){
        Intent intent=new Intent();
        NewsBean nb = new NewsBean();
        nb.setNid(item.getData().getNid());
        nb.setImgs(item.getData().getImgs());
        nb.setTid(item.getData().getTid());

        intent.putExtra("rvalue", item.getData().getRvalue());
        intent.putExtra("tid", item.getData().getTid());
        intent.putExtra("newbean", nb);
        intent.putExtra("nid", item.getData().getNid());
        intent.putExtra("from","history");
        if ("1".equals(item.getData().getRtype())) {

            flag = true;
            intent.setClass(mContext, NewsDetailActivity.class);
        } else if ("2".equals(item.getData().getRtype())) {

            flag = true;
            intent.setClass(mContext, NewsAlbum_Activity.class);
        }else if ("4".equals(item.getData().getRtype())) {

            flag = true;
            intent.setClass(mContext, ZhuantiActivityNew.class);
        }else if ("6".equals(item.getData().getContenttype())) {
            intent.setClass(mContext, ZhuantiActivityNew.class);
        }else if ("6".equals(item.getData().getRtype())) {

            flag = true;
            intent.setClass(mContext, NewsVideoActivity.class);
        }else {
            flag = true;
            intent.setClass(mContext, NewsDetailActivity.class);
        }
        if (flag) {
            mContext.startActivity(intent);
            AAnim.ActivityStartAnimation((Activity) context);
        }
    }

    class ReplyDialog extends Dialog implements View.OnClickListener {
        EditText et_reply;
        TextView tv_send;

        public ReplyDialog(Context context) {
            super(context);
            init();
        }

        public ReplyDialog(@NonNull Context context, @StyleRes int themeResId) {
            super(context, themeResId);
            init();
        }

        private void init() {
            WindowManager wm = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
            int width = wm.getDefaultDisplay().getWidth();
            View view = LayoutInflater.from(mContext).inflate(R.layout.et_reply_laout, null);
            setContentView(view);
            et_reply = view.findViewById(R.id.et_reply);
            tv_send = view.findViewById(R.id.tv_send);
            tv_send.setOnClickListener(this);
            Window dialogWindow = getWindow();
            WindowManager.LayoutParams lp = dialogWindow.getAttributes();
            dialogWindow.setGravity(Gravity.END | Gravity.BOTTOM);
            lp.width = width;
            dialogWindow.setAttributes(lp);
        }

        @Override
        public void onClick(View view) {
            String msg = et_reply.getText().toString().trim();
            if (spu.getUser() == null) {
                PageCtrl.start2LoginActivity((Activity) mContext);
            } else {
                if (msg == null) {
                    TUtils.toast("请输入内容");
                } else {
                    sendComment(msg, cid,nid,type);
                }
            }
        }

        private void sendComment(String content, String cid,String nid,String type) {

            Map<String, String> map = new HashMap<>();
            map.put("nid", nid);
            map.put("uid", spu.getUser().getUid());
            map.put("type", type);
            map.put("title", nid);
            map.put("content", content);
            map.put("cid", cid);
            map.put("json_url", "fsdfsdfsfs");//随便传一个
            map.put("siteid", InterfaceJsonfile.SITEID);
            Factory.provideHttpService()
                    .newsComment(ParamUtils.getRequestParam(map))
                    .onBackpressureBuffer()
                    .subscribeOn(Schedulers.io())
                    .filter(new Func1<ComResultEntity, Boolean>() {
                        @Override
                        public Boolean call(ComResultEntity entity) {
                            LogUtils.i("200-->" + entity.code);
                            if ("200".equals(entity.code)) {
                                LogUtils.i("200-->成功");
                                return true;
                            }
                            LogUtils.i("200失败");
                            return false;
                        }
                    })
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new Action1<ComResultEntity>() {
                        @Override
                        public void call(ComResultEntity entity) {

                            if ("200".equals(entity.code)) {
                                TUtils.toast("评论成功");
                                et_reply.setText("");
                            } else {
                                TUtils.toast(entity.msg);
                            }
//                            setData();
                        }
                    }, new Action1<Throwable>() {
                        @Override
                        public void call(Throwable throwable) {
                            TUtils.toast("评论失败");

                        }
                    });
        }

    }

    public void Priase(String cid, final TextView tv, final ImageView iv) {
        final ComResultBean[] obj = {new ComResultBean()};
        Map<String, String> map = new HashMap<>();
        map.put("uid", spu.getUser().getUid());
        map.put("device", DeviceUtils.getMyUUID(mContext));
        map.put("cid", cid);
        map.put("siteid", InterfaceJsonfile.SITEID);

        Factory.provideHttpService().PraiseOnComment(ParamUtils.getRequestParam(map))
                .onBackpressureBuffer()
                .subscribeOn(Schedulers.io())
                .filter(new Func1<ComResultEntity, Boolean>() {
                    @Override
                    public Boolean call(ComResultEntity emptyEntity) {
                        return true;
                    }
                })
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<ComResultEntity>() {
                    @Override
                    public void call(ComResultEntity emptyEntity) {

                        LogUtils.e("code---" + emptyEntity.code);
                        if (emptyEntity.code.equals("200")) {
                            tv.setText(emptyEntity.data.getPraisenum());
                            tv.setTextColor(mContext.getResources().getColor(R.color.color_df3031));
                            iv.setImageResource(R.drawable.dianzan_selected);
                        } else {
                            TUtils.toast(emptyEntity.msg);
                        }
                    }
                });

    }

}
